//
//  ImmediatelyDDViewController.swift
//  RoundCatPlay2
//
//  Created by 姜澎 on 2019/10/26.
//  Copyright © 2019 jiangpeng. All rights reserved.
//

import UIKit

class ImmediatelyDDViewController: UIViewController {
    
    
    //    组一
    private lazy var contenLable :UILabel = {
        let l = UILabel()
        l.text = "我喜欢玩和平精英,专业吃鸡陪玩哦!快来一起玩吧小哥哥们，我在圆猫陪玩等你奥!"
        l.numberOfLines = 0
        l.textColor = UIColor(red: 151.0 / 255.0, green: 151.0 / 255.0, blue: 151.0 / 255.0, alpha: 1.0)
        l.font = UIFont.systemFont(ofSize: 14)
        return l
    }()
    
    
//     组二
  
    private lazy var jiNengView: GroupView = {
        let v = GroupView()
        v.jnImage.image = UIImage(named: "技能icon")
        v.jnL.text = "我的技能"
        return v
    }()
    
//    3
    private lazy var PingjiaView: GroupView = {
        let v = GroupView()
        v.jnImage.image = UIImage(named: "评价")
        v.jnL.text = "TA的评价"
        return v
    }()
    
//    4
    private lazy var ziLiaoC: GroupView = {
        let v = GroupView()
        v.jnImage.image = UIImage(named: "zlIcon")
        v.jnL.text = "TA的资料"
        return v
    }()
    
    private lazy var ziLiao: ZiLiaoView = {
        let v = ZiLiaoView()
        return v
    }()
    
//    5
    private lazy var FuWu: GroupView = {
        let v = GroupView()
        v.jnImage.image = UIImage(named: "爱心")
        v.jnL.text = "服务承诺"
        return v
    }()
    
//    6
    private lazy var fuWuChengNuo: FuWuChengNuo = {
        let v = FuWuChengNuo()
        return v
    }()
    
//    底部按钮
    private lazy var sld: ShouCangLianXiDingDan = {
        let v = ShouCangLianXiDingDan()
        return v
    }()
    
    private lazy var contentRadio :UIButton = {
        let b = UIButton(type: .custom)
        b.setImage(UIImage(named:"未播放"), for: .normal)
        b.setImage(UIImage(named:"未播放"), for: .selected)
        return b
    }()
    
    private lazy var leftButton:UIButton = {
        let b = UIButton(type: .custom)
        let y = UIApplication.shared.statusBarFrame.size.height
        b.setImage(UIImage(named: "leftBtu"), for: .normal)
        b.setImage(UIImage(named: "leftBtu"), for: .highlighted)
        b.frame = CGRect(x: 23, y:y, width: 50, height: 50)
        b.addTarget(self, action: #selector(returnFunc), for: .touchUpInside)
        b.tag = 1
        return b
    }()
    
    private lazy var rightButton:UIButton = {
        let b = UIButton(type: .custom)
        let y = UIApplication.shared.statusBarFrame.size.height
        b.setImage(UIImage(named: "rightBtu"), for: .normal)
        b.setImage(UIImage(named: "rightBtu"), for: .highlighted)
        b.frame = CGRect(x: UIScreen.main.bounds.width - 50 - 23, y:y, width: 50, height: 50)
        b.addTarget(self, action: #selector(returnFunc), for: .touchUpInside)
        b.tag = 2
        return b
    }()
    
    private lazy var tvc: UITableView = {
        let t = UITableView()
        t.delegate = self
        t.dataSource = self
        t.showsVerticalScrollIndicator = false
        
        t.register(UITableViewCell.self, forCellReuseIdentifier: "onecell")
        t.register(UITableViewCell.self, forCellReuseIdentifier: "rowOne")
        t.register(JiNengTableViewCell.self, forCellReuseIdentifier: "rowTwo")
        t.register(UITableViewCell.self, forCellReuseIdentifier: "groupOne")
        t.register(PingJiaTableViewCell.self, forCellReuseIdentifier: "rowThree")
        t.register(UITableViewCell.self, forCellReuseIdentifier: "groupTwo")
        t.register(UITableViewCell.self, forCellReuseIdentifier: "rowFive")
        t.register(UITableViewCell.self, forCellReuseIdentifier: "groupThree")
        t.register(UITableViewCell.self, forCellReuseIdentifier: "groupFive")
        t.register(UITableViewCell.self, forCellReuseIdentifier: "rowSix")
        t.separatorStyle = .none
        return t
    }()
    
    //    轮播图
    private lazy var cycleView :CycleView = {
        
        let y = -44
        let x = 0
        let width = Int(UIScreen.main.bounds.size.width)
        let height = 320 + 44
        
        let cycleView : CycleView = CycleView(frame: CGRect(x: x, y: y, width: width, height: height))
        cycleView.contentMode = .scaleAspectFit
        cycleView.imageURLStringArr = ["DDRunLoopImage", "DDRunLoopImage", "DDRunLoopImage"]
        return cycleView
    }()
    
    //    topView
    private lazy var topView: DingDanView = {
        var v = DingDanView(frame: CGRect(x: 0, y: -100, width: Int(UIScreen.main.bounds.width), height:100))
        return v
    }()
   
    
    //    主头视图
    private lazy var headView:UIView = {
        let h = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 20))
        h.backgroundColor = UIColor.white
        h.addSubview(self.topView)
        h.addSubview(self.headViewV)
        return h
    }()
    
    //     头视图
    private lazy var headViewV: UIView = {
        let h = UIView(frame: CGRect(x: 0, y: -15, width: UIScreen.main.bounds.width, height: 55))
        h.backgroundColor = UIColor.white
        h.layer.cornerRadius = 20
        h.addSubview(self.titleLbale)
        h.addSubview(self.gxTitle)
        return h
    }()
    //    头标题
    private lazy var titleLbale: UILabel = {
        let l = UILabel()
        l.text = "迷人小妖精"
        l.font = UIFont.systemFont(ofSize: 22)
        l.sizeToFit()
        return l
    }()
    //    个性头标题
    private lazy var gxTitle: UILabel = {
        let l = UILabel()
        l.text = "贴心服务"
        l.textColor = UIColor(red: 71/255, green: 84/255, blue: 233/255, alpha: 1)
        l.backgroundColor = .white
        l.layer.cornerRadius = 4
        l.layer.borderColor = UIColor(red: 71/255, green: 84/255, blue: 233/255, alpha: 1).cgColor
        l.clipsToBounds = true
        l.layer.borderWidth = 1
        l.font = UIFont.systemFont(ofSize: 14)
        l.textAlignment = .center
        return l
    }()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        
        setUI()
        
    }
    //    隐藏导航栏  返回时显示导航栏
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.isTranslucent = false
        // 隐藏首页的导航栏 true 有动画
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // 跳转页面的导航 不隐藏 false
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    
    @objc func returnFunc(button:UIButton){
        if button.tag == 1 {
            self.navigationController?.popViewController(animated: true)
        }else{
            
        }
        
    }
    
}


extension ImmediatelyDDViewController :UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 6
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 2 {
            return 3
        }else if section == 3{
            return 4
        }else if section == 4{
            return 2
        }else if section == 5{
            return 2
        }else {
            return 1
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "onecell")
            cell!.contentView.addSubview(cycleView)
            cell!.backgroundColor = UIColor(red: 249/255.0, green: 249/255.0, blue: 249/255.0, alpha: 1)
            
            return cell!
        }else if indexPath.section == 1{
            let cell = tableView.dequeueReusableCell(withIdentifier: "rowOne")
            cell?.selectionStyle = .none
            cell?.contentView.addSubview(self.contenLable)
            cell?.contentView.addSubview(self.contentRadio)
            setContenLable()
            setRadioBtu()
            return cell!
        }else if indexPath.section == 2 {
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "groupOne")
                cell?.addSubview(jiNengView)
                setJNView()
               
                cell?.selectionStyle = .none
                return cell!
            }
            let cell = tableView.dequeueReusableCell(withIdentifier: "rowTwo") as! JiNengTableViewCell
            cell.selectionStyle = .none
            
            let m = JinNengModel(titleImage: "王者荣耀", titleName: "王者荣耀", duanWei: "铂金尊贵", times: "时长 887", money: "¥40")
            cell.jiNengModel = m
           
            return cell
        } else if indexPath.section == 3 {
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "groupTwo")
                cell?.addSubview(PingjiaView)
                setPingjiaView()
                cell?.selectionStyle = .none
                return cell!
            }
            let cell = tableView.dequeueReusableCell(withIdentifier: "rowThree") as! PingJiaTableViewCell
            let m = PingJiaModel(image: "安小艺", title: "安小艺", content: "喜欢小姐姐的声音，技术还贼6，超赞哦哈", date: "2019/07/01", imageNum: 5)
            cell.count = m.imageNum
            cell.pingJiaModel = m
            cell.selectionStyle = .none
            return cell
        }else if indexPath.section == 4 {
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "groupThree")
                cell?.addSubview(ziLiaoC)
                setziLiaoCView()
                cell?.selectionStyle = .none
                return cell!
            }
            let cell = tableView.dequeueReusableCell(withIdentifier: "rowFive")
            cell?.addSubview(self.ziLiao)
            setZiliaoFrame()
            cell?.selectionStyle = .none
            return cell!
        }else if indexPath.section == 5 {
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "groupFive")
                cell?.addSubview(FuWu)
                setFuWuView()
                cell?.selectionStyle = .none
                return cell!
            }
            let cell = tableView.dequeueReusableCell(withIdentifier: "rowSix")
            cell?.addSubview(fuWuChengNuo)
            setFuWuChengNuo()
            cell?.selectionStyle = .none
            return cell!
            
        }
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 320
        }else {
            return UITableView.automaticDimension
        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1 {
            return 40
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
            return self.headView
        
    }
    

    
//    func scrollViewDidScroll(_ scrollView: UIScrollView) {
//        if scrollView == self.tvc {
//            let sectionHeaderHeight = CGFloat(0)//headerView的高度
//            if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
//
//                scrollView.contentInset = UIEdgeInsets(top: -scrollView.contentOffset.y, left: 0, bottom: 0, right: 0);
//
//            } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
//
//                scrollView.contentInset = UIEdgeInsets(top: -sectionHeaderHeight, left: 0, bottom: 0, right: 0);
//            }
//
//        }
//    }
    
    
    
}


extension ImmediatelyDDViewController {
    func setUI() {
        view.addSubview(self.tvc)
        view.addSubview(self.leftButton)
        view.addSubview(self.rightButton)
        view.addSubview(sld)
        
        setTvcFrame()
        settitleLableFrame()
        setGxTitle()
        setBottomView()
    }
    
    func setTvcFrame() {
        self.tvc.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.right.equalTo(0)
            make.left.equalTo(0)
            make.bottom.equalTo(-100)
        }
    }
    
    func settitleLableFrame(){
        self.titleLbale.snp.makeConstraints { (make) in
            make.top.equalTo(self.headViewV.snp_top).offset(23)
            make.left.equalTo(self.headViewV.snp_left).offset(17)
        }
    }
    
    func setGxTitle() {
        self.gxTitle.snp.makeConstraints { (make) in

            make.top.equalTo(titleLbale.snp_top)
            make.left.equalTo(titleLbale.snp_right).offset(10)
            make.width.equalTo(59)
            make.height.equalTo(22)
        }
    }
    
    func setTopViewFrame() {
        self.topView.snp.makeConstraints { (make) in
            make.top.equalTo(250)
            make.right.equalTo(0)
            make.left.equalTo(0)
            make.height.equalTo(100)
        }
    }
    //    第一组 视图布局
    func setContenLable() {
        self.contenLable.snp.makeConstraints { (make) in
            make.top.equalTo(5)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.bottom.equalTo(self.contentRadio.snp_top).offset(-20)
        }
    }
    
    func setRadioBtu() {
        self.contentRadio.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.bottom.equalTo(-20)
            make.height.equalTo(30)
            make.width.equalTo(150)
            
        }
    }

    func setJNView() {
        self.jiNengView.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.right.equalTo(0)
            make.left.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
    func setPingjiaView() {
        self.PingjiaView.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.right.equalTo(0)
            make.left.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
    func setZiliaoFrame() {
        self.ziLiao.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
    func setziLiaoCView() {
        self.ziLiaoC.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
    func setFuWuView() {
        self.FuWu.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
    func setFuWuChengNuo() {
        self.fuWuChengNuo.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
    }

    func setBottomView() {
        self.sld.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(-34)
            make.height.equalTo(49)
        }
    }
}
